package com.yanqu.road.server.manager.activity.sonhai.handler.impl;

import com.yanqu.road.entity.activity.sonhai.config.SonHaiEventConfig;
import com.yanqu.road.entity.activity.sonhai.config.SonHaiFloorConfig;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiEggData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiWinPlotResp;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.handler.IPlotTypeHandler;
import com.yanqu.road.server.manager.activity.sonhai.handler.entity.PlotTypeHandlerExploreReq;
import com.yanqu.road.server.manager.activity.sonhai.handler.entity.PlotTypeHandlerExploreResp;
import com.yanqu.road.utils.ListHelper;
import com.yanqu.road.utils.RandomHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

/**
 * 兽蛋
 */
public class PlotType13Handler implements IPlotTypeHandler {

    /**
     * logger
     */
    private static Logger logger = LogManager.getLogger(PlotType13Handler.class.getName());

    /**
     * 探索后处理
     * @param req
     * @return
     */
    @Override
    public PlotTypeHandlerExploreResp explore(PlotTypeHandlerExploreReq req) {
        CrossSonHaiUser user = req.getUser();;
        CrossSonHaiActivity activity = req.getActivity();
        SonHaiPlotData plotData = req.getPlotData();
        CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
        SonHaiEventConfig eventConfig = activity.getSonHaiConfig().getEventConfig(plotData.getPlotId());
        SonHaiRegionData regionData = user.getRegionData();
        PlotTypeHandlerExploreResp resp = new PlotTypeHandlerExploreResp();
        SonHaiFloorConfig floorConfig = activity.getSonHaiConfig().getFloorConfig(regionData.getRegionId());
        //地块创建蛋
        SonHaiEggData eggData = this.createEgg(activity, team, user, plotData.getPlotId(), plotData);
        if (eggData == null) {
            logger.error("山海伏兽-探索-蛋事件：蛋池子为空，{}", user.getUserId());
            return null;
        }
        //BUFF:发现异兽栖地块时，获得该地块{0}%的伏兽积分奖励
        long addExtraScore = user.findEggAddScoreFromBuff(eventConfig.getMonsterScore());
        if (addExtraScore > 0) {
            user.addScore(addExtraScore);
        }

        SonHaiWinPlotResp winPlotResp = new SonHaiWinPlotResp();
        //积分
        long addScore = user.addScoreByPlot(regionData, plotData, regionData.getProgress());
        winPlotResp.setAddScore(addScore);

        //寻踪进度
        int addProgress = eventConfig.getSchedule();
        int opValue = user.getProgressOpValue(plotData.getX(), plotData.getY());//加成值
        int opRate = user.getProgressOpRate(regionData);//加成率
        int extraOpValue = user.countExtraProgress(addProgress, opRate, opValue);
        addProgress += extraOpValue;
        regionData.addProgress(addProgress);
        winPlotResp.setAddProgress(addProgress);
        winPlotResp.setExtraAddProgress(extraOpValue);

        //山海灵气
        long baseAddAuraScore = eventConfig.getPvpScore() * floorConfig.getEnergy();
        int auraScoreOpRate = user.getAuraScoreOpRate(plotData, false);//buff:山海灵气加成率
        long auraScoreOpValue = user.getAuraScoreOpValue(plotData, false);//buff:山海灵气加成值
        long addAuraScore = user.countAureScore(baseAddAuraScore, auraScoreOpRate, auraScoreOpValue);
        team.changeAuraScore(user, addAuraScore);
        winPlotResp.setAddAuraScore(addAuraScore);

        resp.setWinPlotResp(winPlotResp);
        return resp;
    }

    /**
     * 地块注入蛋
     * @param activity
     * @param team
     * @param user
     * @param plotData
     * @return
     */
    public SonHaiEggData createEgg(CrossSonHaiActivity activity, CrossSonHaiTeam team, CrossSonHaiUser user, int eggEventId, SonHaiPlotData plotData) {
        //蛋池子
        Map<Integer, Set<Integer>> eggPool = activity.getRandomEggPool(user, team);
        Set<Integer> eggIds = eggPool.get(eggEventId);
        if (eggIds == null || eggIds.isEmpty()) {
            return null;
        }
        //随机
        Integer eggId = RandomHelper.getRandomElement(eggIds);
        if (eggId == null) {
            return null;
        }
        SonHaiEggData eggData = activity.getEgg(eggId);
        //判断蛋是否达到上限，需要移除最早发现的蛋
        int eggNumMax = activity.getSonHaiConfig().getEggSharedNum();//读配置
        Set<Integer> canBattleEggPool = team.getCanBattleEggPool();
        if (canBattleEggPool.size() >= eggNumMax) {
            //移除最早的蛋
            team.removeOldEgg();
        }
        //地块注入蛋
        plotData.setEggId(eggData.getId());
        //蛋注入地块
        eggData.addAttackMeUserPlot(plotData);
        if (eggData.getFindUserId() <= 0) {
            eggData.setFindUserId(user.getUserId());
        }
        return eggData;
    }
}
